﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TheadTest
{
    class Program
    {
        static void Main(string[] args)
        {
            MyThread myThread = new MyThread();
            ///空参实例化线程 
            ///用实例化的委托ThreadStart来构造
            ///委托原型：public delegate void ThreadStart()
            ///实例化委托时，要用一个空参的方法来构造
            Thread subThreadOne = new Thread(new ThreadStart(myThread.Run));

            ///参数数实例化线程
            ///委托原型：public delegate void ParameterizedThreadStart(object obj);
            ///实例化委托时，要用带一个object类型参数的方法来构造
            Thread subThreadTwo = new Thread(new ParameterizedThreadStart(myThread.Run));
            //启动子线程1
            subThreadOne.Start();

            ///线程启动时，传参数
            string mes = "（参数A）";
            subThreadTwo.Start(mes);
            for (int i = 0; i < 5; i++)
            {
                //当前线程(主线程)睡眠
                Thread.Sleep(2);
                Console.WriteLine("主线程。。。");
            }
            //放弃子线程1
            subThreadOne.Abort();
            //放弃子线程2
            subThreadTwo.Abort();

            Console.ReadKey();
        }
    }
    class MyThread
    {
        public void Run()
        {
            while (true)
            {
                Console.WriteLine("第1个MyThread运行....");
                //当前线程(子线程1)睡眠
                Thread.Sleep(5);
            }
        }
        public void Run(object obj)
        {
            while (true)
            {
                Console.WriteLine(string.Format("第2个MyThread运行....{0}", obj.ToString()));
                //当前线程(子线程2)睡眠
                Thread.Sleep(5);
            }
        }
    }
}
